import os
from pathlib import Path
import requests

ASSETS = [
    ("swagger-ui.css", "https://cdn.jsdelivr.net/npm/swagger-ui-dist/swagger-ui.css"),
    ("swagger-ui-bundle.js", "https://cdn.jsdelivr.net/npm/swagger-ui-dist/swagger-ui-bundle.js"),
    ("swagger-ui-standalone-preset.js", "https://cdn.jsdelivr.net/npm/swagger-ui-dist/swagger-ui-standalone-preset.js"),
    ("redoc.standalone.js", "https://cdn.jsdelivr.net/npm/redoc/bundles/redoc.standalone.js"),
]

def ensure_assets(base_dir: str = "static/docs"):
    p = Path(base_dir)
    p.mkdir(parents=True, exist_ok=True)
    for name, url in ASSETS:
        fp = p / name
        if not fp.exists() or fp.stat().st_size == 0:
            r = requests.get(url, timeout=30)
            r.raise_for_status()
            fp.write_bytes(r.content)